/**
 * @author: XXD
 * @date:2017-07-08
 */
$(document).ready(
    function() {
        var VAL = {
            NORMAL : "0000000",
            T_FIPINQBOX : "FIPINQBOX",
            T_FSPBOXCHGDEF : "FSPBOXCHGDEF",
            DISABLED_ATTR : {
                'disabled' : true
            },
            ENABLED_ATTR : {
                'disabled' : false
            },
            importFlg : "N",
            // EVT_USER :$("#userId").text(),
            EVT_USER    :"C1706004",
        };
        var domObj = {
            $boxIdFkA : $("#boxIdFkA"),
            $boxIdFkB : $("#boxIdFkB"),
            $note: $("#note"),

            buttons : {
                $query_btn : $("#query_btn"),
                $save_btn : $("#save_btn"),
                $register_btn : $("#register_btn"),
                $export_btn : $("#export_btn"),
                $import_btn : $("#import_btn"),
                $tempDown_btn : $("#tempDown_btn"),
                $delete_btn : $("#delete_btn"),
                $toLeft_btn : $("#toLeft_btn"),
                $toRight_btn : $("#toRight_btn"),
                $upload_btn : $("#upload_btn")
            },
            grids : {
                $retPrdAListGrd : $("#retPrdAListGrd"),
                $retPrdBListGrd : $("#retPrdBListGrd"),
                $retBoxChgGrd : $("#retBoxChgGrd")
            },
            dialogs : {

            }

        };

        //1.初始化grid
        var initGrid = {
            initRetPrdAListGrd : function (){
                domObj.grids.$retPrdAListGrd.jqGrid({
                    url : "",
                    datatype : "local",
                    mtype : "POST",
                    height : 530,
                    width :  $("#retPrdAListDiv").width()*0.99,
                    autowidth : true,
                    shrinkToFit : true	,
                    resizable : true,
                    loadonce : true,
                    viewrecords : true,
                    rownumbers : true,
                    rowNum : 20,
                    emptyrecords : true,
                    multiselect : false,
                    pager : $("#retPrdAListPg"),
                    colModel : [ {
                        name : 'slot_no',
                        index : 'slot_no',
                        label : '子格位',
                        width : 50
                    }, {
                        name : 'slot_noLt',
                        index : 'slot_noLt',
                        label : '上一子格位',
                        width : 50,
                        hidden: true
                    },{
                        name : 'prd_seq_id',
                        index : 'prd_seq_id',
                        label : '当前产品代码',
                        width : 100
                    }, {
                        name : 'prd_seq_idB',
                        index : 'prd_seq_idB',
                        label : '设定后产品代码',
                        width : 100
                    }]
                });
            },

            initRetPrdBListGrd : function (){
                domObj.grids.$retPrdBListGrd.jqGrid({
                    url : "",
                    datatype : "local",
                    mtype : "POST",
                    height : 530,
                    width : $("#retPrdBListDiv").width()*0.99,
                    autowidth : true,
                    shrinkToFit : true,
                    resizable : true,
                    loadonce : true,
                    viewrecords : true,
                    rownumbers : true,
                    rowNum : 20,
                    emptyrecords : true,
                    multiselect : false,
                    pager : $("#retPrdBListPg"),
                    colModel : [ {
                        name : 'slot_no',
                        index : 'slot_no',
                        label : '子格位',
                        width : 50
                    }, {
                        name : 'slot_noLt',
                        index : 'slot_noLt',
                        label : '上一子格位',
                        width : 50,
                        hidden: true
                    }, {
                        name : 'prd_seq_id',
                        index : 'prd_seq_id',
                        label : '当前产品代码',
                        width : 100
                    }, {
                        name : 'prd_seq_idA',
                        index : 'prd_seq_idA',
                        label : '设定后产品代码',
                        width : 100
                    }],

                });
            },

            initRetBoxChgGrd : function () {
                domObj.grids.$retBoxChgGrd.jqGrid({
                    url : "",
                    datatype : "local",
                    mtype : "POST",
                    height : 530,
                    width : $("#retBoxChgDiv").width()*0.99,
                    autowidth : true,
                    shrinkToFit : true,
                    resizable : true,
                    loadonce : true,
                    viewrecords : true,
                    rownumbers : true,
                    rowNum : 20,
                    emptyrecords : true,
                    multiselect : true,
                    pager : $("#retBoxChgPg"),
                    colModel : [{
                        name : 'box_chg_seq_id',
                        index : 'box_chg_seq_id',
                        label : 'BOX拆并流水号',
                        width : 60,
                        hidden: true
                    },{
                        name : 'fr_slot_no_fk',
                        index : 'fr_slot_no_fk',
                        label : '源子格位',
                        width : 60
                    },{
                        name : 'fr_box_id_fk',
                        index : 'fr_box_id_fk',
                        label : '源箱号',
                        width : 100
                    },{
                        name : 'to_slot_no_fk',
                        index : 'to_slot_no_fk',
                        label : '目标子格位',
                        width : 80
                    },{
                        name : 'to_box_id_fk',
                        index : 'to_box_id_fk',
                        label : '目标箱号',
                        width : 100
                    },{
                        name : 'evt_usr',
                        index : 'evt_usr',
                        label : '设定人',
                        width : 100
                    },{
                        name : 'evt_timestamp',
                        index : 'evt_timestamp',
                        label : '设定时间',
                        width : 150
                    },
                        {
                            name : 'evt_note',
                            index : 'evt_note',
                            label : '备注',
                            width : 130
                        }]
                });
            }
        };
        initGrid.initRetPrdAListGrd();
        initGrid.initRetPrdBListGrd();
        initGrid.initRetBoxChgGrd();

        function  getAddRowID($grid) {
            //返回当前grid里所有数据的id
            var rowIds = $grid.jqGrid("getDataIDs");
            //如果行数大于0，那么就返回最后一个行的行数，否则就返回为第一行。
            return rowIds.length > 0 ? (parseInt(rowIds[rowIds.length - 1])) + 1 : "1";
        };

        //数字补0
        function PrefixInteger(num, length) {
            return (Array(length).join('0') + num).slice(-length);
        };

        var buttonFnc = {
            queryFnc : function (){
                var inObj, outObj, tblCnt, boxSetCodeA,boxSetCodeB;
                boxSetCodeA = domObj.$boxIdFkA.val().trim();
                boxSetCodeB = domObj.$boxIdFkB.val().trim();
                iary = {
                    box_id_fkA : boxSetCodeA,
                };
                iaryB = {
                    box_id_fkB :boxSetCodeB,
                };
                inObj = {
                    trx_id : VAL.T_FSPBOXCHGDEF,
                    action_flg : 'Q',
                    iary : [iary],
                    iaryb :[iaryB]
                },
                    outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    setGridInfo(outObj.oary, "#retBoxChgGrd",
                        true);
                }
            },

            saveFnc : function (){
                var boxSetCodeA = domObj.$boxIdFkA.val().trim();
                var boxSetCodeB = domObj.$boxIdFkB.val().trim();
                var note = domObj.$note.val().trim();
                if(!boxSetCodeA){
                    showErrorDialog("", "卡匣代码A不能为空");
                    return;
                }
                if(!boxSetCodeB){
                    showErrorDialog("", "卡匣代码B不能为空");
                    return;
                }
                if(!note){
                    showErrorDialog("", "备注信息不能为空");
                    return;
                }
                var rowIdAs = domObj.grids.$retPrdAListGrd.jqGrid("getDataIDs");
                var rowIdBs = domObj.grids.$retPrdBListGrd.jqGrid("getDataIDs");
                var iary = [];
                for(var i = 0;i < rowIdAs.length; i++){
                    var rowData = domObj.grids.$retPrdAListGrd.jqGrid("getRowData",rowIdAs[i]);
                    if(rowData.prd_seq_idB != ""){
                        iary.push({
                            box_id_fkA : boxSetCodeA,
                            prd_seq_id : rowData.prd_seq_idB,//当前玻璃
                            fr_box_id_fk : boxSetCodeB,
                            fr_slot_no_fk : rowData.slot_noLt,
                            to_box_id_fk : boxSetCodeA,
                            to_slot_no_fk : rowData.slot_no
                        });
                    }
                }
                var iaryB = [];
                for(var i = 0;i < rowIdBs.length; i++){
                    var rowData = domObj.grids.$retPrdBListGrd.jqGrid("getRowData",rowIdBs[i]);
                    if(rowData.prd_seq_idA != ""){
                        iaryB.push({
                            box_id_fkB : boxSetCodeB,
                            prd_seq_id : rowData.prd_seq_idA,//当前玻璃
                            fr_box_id_fk : boxSetCodeA,
                            fr_slot_no_fk : rowData.slot_noLt,
                            to_box_id_fk : boxSetCodeB,
                            to_slot_no_fk : rowData.slot_no
                        });
                    }
                }

                if(iary.length == 0 && iaryB.length == 0){
                    showErrorDialog("","取样模板列表为空，不能保存！");
                    return false;
                }
                var inObj = {
                    trx_id : VAL.T_FSPBOXCHGDEF,
                    action_flg : 'S',
                    box_idA :boxSetCodeA,
                    box_idB :boxSetCodeB,
                    iary : iary,
                    iaryB :iaryB,
                    evt_usr : VAL.EVT_USER,
                    evt_note : domObj.$note.val().trim()
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    showSuccessDialog("模板设定成功！");
                }
            },

            registerFnc : function (){
                var note = domObj.$note.val().trim();
                if(!note){
                    showErrorDialog("", "备注信息不能为空");
                    return;
                }
                var rowDatas = domObj.grids.$retBoxChgGrd.getRowData();
                var iary = [];
                for (var i = 0; i < rowDatas.length; i++){
                    iary.push({
                        fr_box_id_fk : rowDatas[i].fr_box_id_fk,
                        fr_slot_no_fk : rowDatas[i].fr_slot_no_fk,
                        to_box_id_fk : rowDatas[i].to_box_id_fk,
                        to_slot_no_fk : rowDatas[i].to_slot_no_fk,
                    });
                }

                if(iary.length == 0){
                    showErrorDialog("","登记模板列表模板为空，不能登记！");
                    return false;
                }
                var inObj = {
                    trx_id : VAL.T_FSPBOXCHGDEF,
                    action_flg : 'R',
                    iary : iary,
                    evt_usr : VAL.EVT_USER,
                    evt_note : domObj.$note.val().trim()
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    showSuccessDialog("模板登记成功！");
                }

            },

            exportFnc : function (){
                var selectedIds = domObj.grids.$retBoxChgGrd.jqGrid("getGridParam", "selarrrow");
                if(selectedIds.length == 0){
                    showErrorDialog("","请先从列表中选择需要导出的模板信息!");
                    return false;
                };
                var iary = [];
                for(var i = 0; i< selectedIds.length;i++){
                    var rowData = domObj.grids.$retBoxChgGrd.jqGrid("getRowData",selectedIds[i]);
                    iary.push({
                        fr_box_id_fk : rowData.fr_box_id_fk,
                        fr_slot_no_fk : rowData.fr_slot_no_fk,
                        to_box_id_fk : rowData.to_box_id_fk,
                        to_slot_no_fk : rowData.to_slot_no_fk,
                    });
                }

                inObj = {
                    trx_id : VAL.T_FSPBOXCHGDEF,
                    action_flg : 'G',
                    iary : iary,
                    evt_usr : VAL.EVT_USER,
                    evt_note : domObj.$note.val().trim()
                },
                    outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    showSuccessDialog("导出模板成功！");
                }
            },

            exportE : function () {
                if ($("#excelForm").length > 0) {
                    $("#excelForm").remove();
                }

                var selectedIds = domObj.grids.$retBoxChgGrd.jqGrid("getGridParam", "selarrrow");
                if(selectedIds.length == 0){
                    showErrorDialog("","请先从列表中选择需要导出的模板信息!");
                    return false;
                };
                var evtNote = domObj.$note.val().trim();
                /* var str = '<form id="excelForm" action="exportExcel.do" method="post">';
                 for(var i = 0; i< selectedIds.length;i++) {
                 var rowData = domObj.grids.$retBoxChgGrd.jqGrid("getRowData", selectedIds[i]);
                 str = str + '<input type="hidden" name="SortPo['+i+'].fr_box_id_fk" ' + 'value='+rowData.fr_box_id_fk+'/>';
                 str = str + '<input type="hidden" name="SortPo['+i+'].fr_slot_no_fk" ' + 'value='+rowData.fr_slot_no_fk+'/>';
                 str = str + '<input type="hidden" name="SortPo['+i+'].to_box_id_fk" ' + 'value='+rowData.to_box_id_fk+'/>';
                 str = str + '<input type="hidden" name="SortPo['+i+'].to_slot_no_fk" ' + 'value='+rowData.to_slot_no_fk+'/>';
                 str = str + '<input type="hidden" name="SortPo['+i+'].evtNote" ' + 'value='+evtNote+'/>';
                 }
                 str = str + "</form>";
                 $(str).appendTo("body");
                 $("#excelForm").submit();*/

                var sortList = new Array();
                for(var i = 0; i< selectedIds.length;i++){
                    var rowData = domObj.grids.$retBoxChgGrd.jqGrid("getRowData", selectedIds[i]);
                    sortList.push({fr_box_id_fk :rowData.fr_box_id_fk,fr_slot_no_fk:rowData.fr_slot_no_fk,
                        to_box_id_fk : rowData.to_box_id_fk ,to_slot_no_fk :rowData.to_box_id_fk,
                        evtNote :evtNote});
                }

                $.ajax({
                    type: "POST",
                    url: "exportExcel.do",
                    data: JSON.stringify(sortList),//将对象序列化成JSON字符串
                    dataType:"json",
                    contentType : 'application/json;charset=utf-8', //设置请求头信息
                    success: function(data){
                        /*var url = data;
                         $(document.body).append("<iframe height='0' width='0' frameborder='0'  src=" + url + "></iframe>")*/
                        if(data == "success"){
                            window.location.href="exportExcel.do";
                        }
                    }

                });

            },

            importFnc : function (obj){
                /*
                 FileReader共有4种读取方法：
                 1.readAsArrayBuffer(file)：将文件读取为ArrayBuffer。
                 2.readAsBinaryString(file)：将文件读取为二进制字符串
                 3.readAsDataURL(file)：将文件读取为Data URL
                 4.readAsText(file, [encoding])：将文件读取为文本，encoding缺省值为'UTF-8'
                 */
                var wb;//读取完成的数据
                var rABS = false; //是否将文件读取为二进制字符串
                if(!obj.files) {
                    return;
                }
                var fileName = obj.value;
                if(fileName.length > 1 && fileName ) {
                    var ldot = fileName.lastIndexOf(".");
                    var type = fileName.substring(ldot + 1);
                    if(!(type=="xlsx"||type=="xls")) {
                        showErrorDialog("", "只能上传excel文件");
                        return;
                    }
                    /*var length = fileName.lastIndexOf("\\");
                     var name = fileName.substring(length + 1);
                     if(name!=null) {
                     domObj.buttons.$file_name.val(name);
                     }*/

                }
                var f = obj.files[0];
                var reader = new FileReader();
                reader.onload = function(e) {
                    var data = e.target.result;
                    if(rABS) {
                        wb = XLSX.read(btoa(fixdata(data)), {//手动转化
                            type: 'base64'
                        });
                    } else {
                        wb = XLSX.read(data, {
                            type: 'binary'
                        });
                    }
                    //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
                    //wb.Sheets[Sheet名]获取第一个Sheet的数据
                    var data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
                    for(var i = 0;i < data.length; i++){
                        var newRowId = getAddRowID(domObj.grids.$retBoxChgGrd);
                        domObj.grids.$retBoxChgGrd.jqGrid('addRowData',newRowId,{'fr_slot_no_fk':PrefixInteger(parseInt(data[i].源子格位),2),'fr_box_id_fk':data[i].源箱号,
                            'to_slot_no_fk':PrefixInteger(parseInt(data[i].目标子格位),2),'to_box_id_fk':data[i].目标箱号,'evt_note':data[i].备注});
                    }

                    /*  setGridInfo(data, "#retBoxChgGrd");*/
                    // document.getElementById("demo").innerHTML= JSON.stringify( XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) );
                };
                if(rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
                function fixdata(data) { //文件流转BinaryString
                    var o = "", l = 0, w = 10240;
                    for(; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
                    o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
                    return o;
                }
            },

            select_file: function() {
                document.getElementById("upload_btn").click();
            },

            tempDownFnc : function (){
                if ($("#downForm").length > 0) {
                    $("#downForm").remove();
                }
                var str = '<form id="downForm" action="download.do" method="post">';
                str = str + '<input type="hidden" name="fileName" id= "fileName" />';
                str = str + "</form>";

                $(str).appendTo("body");
                $("#fileName").val("Sorter_Temple.xlsx");
                $("#downForm").submit();
            },

            deleteFnc : function () {
                var selectedIds = domObj.grids.$retBoxChgGrd.jqGrid("getGridParam", "selarrrow");
                if(!selectedIds){
                    showErrorDialog("", "请选择需要删除的信息！");
                    return;
                }
                $("#delete_btn").showCallBackWarnningDialog({
                    errMsg: "是否删除选中信息,请确认!!!!",
                    callbackFn: function (data) {
                        if (data.result == true) {
                            var iary = [];
                            for(var i = 0; i< selectedIds.length;i++){
                                var rowData = domObj.grids.$retBoxChgGrd.jqGrid("getRowData",selectedIds[i]);
                                iary.push({
                                    box_chg_seq_id : rowData.box_chg_seq_id
                                });
                            }
                            inObj = {
                                trx_id : VAL.T_FSPBOXCHGDEF,
                                action_flg : 'D',
                                iary : iary
                            },
                                outObj = comTrxSubSendPostJson(inObj);
                            if (outObj.rtn_code === VAL.NORMAL) {
                                var length = selectedIds.length;
                                for(var i = 0 ;i < length ;i++){
                                    domObj.grids.$retBoxChgGrd.jqGrid("delRowData", selectedIds[0]);
                                }
                                showSuccessDialog("删除信息成功！");
                            }
                        }
                    }
                });
            },

            toLeftFnc : function (){
                var selRowIdB = domObj.grids.$retPrdBListGrd.jqGrid("getGridParam","selrow");
                if(!selRowIdB){
                    showErrorDialog("", "请选择B卡匣中的产品!");
                    return false;
                }
                var rowData =domObj.grids.$retPrdBListGrd.jqGrid("getRowData",selRowIdB);
                if(!rowData.prd_seq_id){
                    showErrorDialog("", "请选择B卡匣中的有玻璃的子格位！");
                    return false;
                }

                var selRowIdA = domObj.grids.$retPrdAListGrd.jqGrid("getGridParam","selrow");
                if(!selRowIdA){
                    showErrorDialog("", "请选择A卡匣中任意一行!");
                    return false;
                }
                var rowDataA =domObj.grids.$retPrdAListGrd.jqGrid("getRowData",selRowIdA);
                if(rowDataA.prd_seq_idB){
                    showErrorDialog("", "请选择A卡匣中还未设定的子格位！");
                    return false;
                }
                var rowDatas = domObj.grids.$retPrdAListGrd.getRowData();
                for(var i = 0; i < rowDatas.length ; i++){
                    if(rowDatas[i].prd_seq_idB == rowData.prd_seq_id){
                        showErrorDialog("", "请重新选择B卡匣其他子格位，当前子格位已经移入A卡匣！");
                        return false;
                    }
                }
                //设置A卡匣 设定后产品代码
                domObj.grids.$retPrdAListGrd.jqGrid(
                    "setCell", selRowIdA, 4,
                    rowData.prd_seq_id);

                domObj.grids.$retPrdAListGrd.jqGrid(
                    "setCell", selRowIdA, 2,
                    rowData.slot_no);
                var a = rowData;
            },

            toRightFnc : function (){
                var selRowIdA = domObj.grids.$retPrdAListGrd.jqGrid("getGridParam","selrow");
                if(!selRowIdA){
                    showErrorDialog("", "请选择A卡匣中的产品!");
                    return false;
                }
                var rowData =domObj.grids.$retPrdAListGrd.jqGrid("getRowData",selRowIdA);
                if(!rowData.prd_seq_id){
                    showErrorDialog("", "请选择A卡匣中的有玻璃的子格位！");
                    return false;
                }

                var selRowIdB = domObj.grids.$retPrdBListGrd.jqGrid("getGridParam","selrow");
                if(!selRowIdB){
                    showErrorDialog("", "请选择B卡匣中任意一行!");
                    return false;
                }
                var rowDataB =domObj.grids.$retPrdBListGrd.jqGrid("getRowData",selRowIdB);
                if(rowDataB.prd_seq_idA){
                    showErrorDialog("", "请选择B卡匣中还未设定的子格位！");
                    return false;
                }
                var rowDatas = domObj.grids.$retPrdBListGrd.getRowData();
                for(var i = 0; i < rowDatas.length ; i++){
                    if(rowDatas[i].prd_seq_idA == rowData.prd_seq_id){
                        showErrorDialog("", "请重新选择A卡匣其他子格位，当前子格位已经移入B卡匣！");
                        return false;
                    }
                }
                //设置B卡匣 设定后产品代码
                domObj.grids.$retPrdBListGrd.jqGrid(
                    "setCell", selRowIdB, 4,
                    rowData.prd_seq_id);

                domObj.grids.$retPrdBListGrd.jqGrid(
                    "setCell", selRowIdB, 2,
                    rowData.slot_no);

            }
        };

        domObj.buttons.$query_btn.bind('click',buttonFnc.queryFnc);
        domObj.buttons.$save_btn.bind('click',buttonFnc.saveFnc);
        domObj.buttons.$register_btn.bind('click',buttonFnc.registerFnc);
        domObj.buttons.$export_btn.bind('click',buttonFnc.exportFnc);
        //domObj.buttons.$export_btn.bind('click',buttonFnc.exportE);

        domObj.buttons.$import_btn.bind('click',
            buttonFnc.select_file);
        domObj.buttons.$tempDown_btn.bind('click',buttonFnc.tempDownFnc);
        domObj.buttons.$delete_btn.bind('click',buttonFnc.deleteFnc);
        domObj.buttons.$toLeft_btn.bind('click',buttonFnc.toLeftFnc);
        domObj.buttons.$toRight_btn.bind('click',buttonFnc.toRightFnc);
        domObj.buttons.$upload_btn.change(function() {
            buttonFnc.importFnc(this);
        });

        domObj.$boxIdFkA.keydown(function(event) {
            var inObj, outObj, tblCnt, boxSetCodeA,boxSetCodeB;
            boxSetCodeA = domObj.$boxIdFkA.val().trim();
            boxSetCodeB = domObj.$boxIdFkB.val().trim();
            if (event.keyCode == 13) {
                if (boxSetCodeA == "") {
                    showErrorDialog("", "请输入卡匣A!");
                    return false;
                }
                if(boxSetCodeA == boxSetCodeB && boxSetCodeA != ""){
                    showErrorDialog("", "卡匣A和卡匣B不能相同，请重新输入!");
                    return false;
                }
                inObj = {
                    trx_id : VAL.T_FIPINQBOX,
                    action_flg : 'Q',
                    box_id : boxSetCodeA,
                    /*box_idB :boxSetCodeB,*/
                    /*tbl_cnt : 1*/
                }, outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    domObj.grids.$retPrdAListGrd.jqGrid('clearGridData');
                    var maxCnt = outObj.max_cnt;//总的子格位
                    if(maxCnt > 0){
                        for(var i = 1; i < parseInt(maxCnt) + 1 ;i++){
                            var newRowId = getAddRowID(domObj.grids.$retPrdAListGrd);
                            if(typeof(outObj.oary) !== "undefined") {
                                var prdCount = outObj.oary.length;//箱子里的玻璃个数
                                for (var j = 0; j < prdCount; j++) {
                                    if (outObj.oary[j].slot_no.replace(/\b(0+)/gi, "")
                                        == i) {
                                        rowDa = {
                                            slot_no: PrefixInteger(i, 2),
                                            prd_seq_id: outObj.oary[j].prd_seq_id,
                                            prd_seq_idB: ""
                                        };
                                        break;
                                    } else {
                                        rowDa = {
                                            slot_no: PrefixInteger(i, 2),
                                            prd_seq_id: "",
                                            prd_seq_idB: ""
                                        };
                                        continue;
                                    }
                                }
                            }else {
                                rowDa = {
                                    slot_no : PrefixInteger(i,2),
                                    prd_seq_id : "",
                                    prd_seq_idB : ""
                                };
                            }

                            domObj.grids.$retPrdAListGrd.jqGrid(
                                "addRowData", newRowId, rowDa);
                        }
                    }
                }
            }
        });

        domObj.$boxIdFkB.keydown(function(event) {
            var inObj, outObj, tblCnt,boxSetCodeA, boxSetCodeB;
            boxSetCodeA = domObj.$boxIdFkA.val().trim();
            boxSetCodeB = domObj.$boxIdFkB.val().trim();
            if (event.keyCode == 13) {
                if (boxSetCodeB == "") {
                    showErrorDialog("", "请输入卡匣B!");
                    return false;
                }
                if(boxSetCodeA == boxSetCodeB && boxSetCodeB !=""){
                    showErrorDialog("", "卡匣A和卡匣B不能相同，请重新输入!");
                    return false;
                }
                inObj = {
                    trx_id : VAL.T_FIPINQBOX,
                    action_flg : 'Q',
                    /*box_idA :boxSetCodeA,*/
                    box_id : boxSetCodeB,
                }, outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    //清空表格
                    domObj.grids.$retPrdBListGrd.jqGrid('clearGridData');
                    var maxCnt = outObj.max_cnt;//总的子格位
                    if(maxCnt > 0){
                        for(var i = 1; i < parseInt(maxCnt) + 1 ;i++){
                            var newRowId = getAddRowID(domObj.grids.$retPrdBListGrd);
                            if(typeof(outObj.oary) !== "undefined"){
                                var prdCount = outObj.oary.length;//箱子里的玻璃个数
                                for(var j = 0;j < prdCount;j++){
                                    if(outObj.oary[j].slot_no.replace(/\b(0+)/gi,"")
                                        == i ){
                                        rowDa = {
                                            slot_no : PrefixInteger(i,2),
                                            prd_seq_id : outObj.oary[j].prd_seq_id,
                                            prd_seq_idB : ""
                                        };
                                        break;
                                    }else{
                                        rowDa = {
                                            slot_no : PrefixInteger(i,2),
                                            prd_seq_id : "",
                                            prd_seq_idB : ""
                                        };
                                        continue;
                                    }
                                }
                            }else{
                                rowDa = {
                                    slot_no : PrefixInteger(i,2),
                                    prd_seq_id : "",
                                    prd_seq_idB : ""
                                };
                            }

                            domObj.grids.$retPrdBListGrd.jqGrid(
                                "addRowData", newRowId, rowDa);
                        }
                    }

                }
            }
        });

        var otherActionBind = function(){
            //Stop from auto commit
            $("form").submit(function(){
                return false;
            });

        };
        function initFunc(){
            /* init();
             iniButtonAction();
             initSettingGrid();*/
            otherActionBind();
        }
        initFunc();
        //模态框拖动
        $(".modal-content").draggable();
        //表格自适应
        function resizeFnc(){
            var offsetBottom, grdDivWidth,offsetBottom1, grdDivWidth1,offsetBottom2, grdDivWidth2;
            //1
            grdDivWidth = $("#table1Div").width();
            offsetBottom = $(window).height() - $("#retPrdAListDiv").offset().top;

            $("#retPrdAListDiv").width(grdDivWidth*0.98);
            $("#retPrdAListDiv").height(offsetBottom*0.99);
            $("#retPrdAListGrd").setGridWidth(grdDivWidth*0.97);
            $("#retPrdAListGrd").setGridHeight(offsetBottom*0.95-51);
            //2
            grdDivWidth1 = $("#table2Div").width();
            offsetBottom1 = $(window).height() - $("#retPrdBListDiv").offset().top;

            $("#retPrdBListDiv").width(grdDivWidth1*0.98);
            $("#retPrdBListDiv").height(offsetBottom1*0.99);
            $("#retPrdBListGrd").setGridWidth(grdDivWidth1*0.97);
            $("#retPrdBListGrd").setGridHeight(offsetBottom1*0.95-51);
            //3
            grdDivWidth2 = $("#table3Div").width();
            offsetBottom2 = $(window).height() - $("#retBoxChgDiv").offset().top;

            $("#retBoxChgDiv").width(grdDivWidth2*0.98);
            $("#retBoxChgDiv").height(offsetBottom2*0.99);
            $("#retBoxChgGrd").setGridWidth(grdDivWidth2*0.97);
            $("#retBoxChgGrd").setGridHeight(offsetBottom2*0.95-51);
        };
        resizeFnc();
        $(window).resize(function() {
            resizeFnc();
        });

    })